package com.baturu.offlineFord;

import com.baturu.common.guava2.Lists2;
import com.baturu.ford.carvin.dal.CarType1DAO;
import com.baturu.ford.carvin.dal.DealVinDAO;
import com.baturu.offlineFord.dto.BtrDTO;
import com.baturu.offlineFord.dto.CarTypeDTO;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: chenjiahao
 * @Time: 2019/1/21
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath*:config/spring-*-dataSource.xml",
        "classpath:spring-service.xml",
        "classpath*:spring-service-debugger.xml",})
public class CarType {
    @Autowired
    private CarType1DAO carTypeDAO;
    @Autowired
    private DealVinDAO vinDAO;

    @Test
    public void testIntercepter() throws Exception {
        carTypeDAO.test();

    }

    @Test
    public void outCarType() throws Exception {

        Integer integer = vinDAO.queryMaxId();
        List<BtrDTO> btrDTOS =carTypeDAO.queryBtrid();
        Set<CarTypeDTO> carTypeDTOS = dealNewCarToDB(integer, btrDTOS);
//        if (CollectionUtils.isNotEmpty(carTypeDTOS)) {
//            autoCreatNewCar(carTypeDTOS,btrDTOS);
//        }

    }

    private void autoCreatNewCar(List<CarTypeDTO> carTypeDTOS, List<BtrDTO> btrDTOS) {
        List<String> optionCode =  Lists2.transform(btrDTOS, BtrDTO::getOptionCode);
        List<CarTypeDTO> c = carTypeDTOS.stream().filter(d -> optionCode.contains(d.getOptionCode())).collect(Collectors.toList());

    }

    private Set<CarTypeDTO> dealNewCarToDB(Integer integer, List<BtrDTO> btrDTOS) {
        Set<CarTypeDTO> c = Sets.newHashSet();
        List<String> btrid =  Lists2.transform(btrDTOS, BtrDTO::getBtrId);
        Integer start = 0;
        for (Integer i = 10000; i < integer;i+=10000) {
            c.addAll(carTypeDAO.queryCarType(start,i));
//            c.addAll(carTypeDAO.queryCarType(0,10000));
            start=i;
        }
        c.addAll(carTypeDAO.queryCarType(start,integer));
        c.removeIf(q->btrid.contains(q.getBtrid()));
        c.forEach(d -> {
            String label = d.getDriven();
            if (StringUtils.isNotBlank(label)) {
                if (label.contains("2WD") || label.contains("FWD")) {
                    d.setDriven( "前轮驱动");
                } else if (label.contains("4WD")) {
                    d.setDriven("四轮驱动");
                } else if (label.contains("RWD")) {
                    d.setDriven("后轮驱动");
                }
            }

        });

        carTypeDAO.insertCarType(c);
        return c;
    }
}
